package com.ahao.aggregation;

import android.content.Context;

import com.ahao.abstraction.PayParams;
import com.ahao.abstraction.LoginResult;
import com.ahao.aggregation.dialog.CustomLoginDialog;
import com.ahao.aggregation.dialog.CustomPaymentDialog;
import com.ahao.aggregation.entry.UserInfo;
import com.ahao.aggregation.utils.LogUtils;
import com.ahao.aggregation.utils.SimpleHttpGetRequest;

import org.json.JSONArray;
import org.json.JSONObject;

public class AHAOUiManager {
    private static AHAOUiManager instance;

    public static AHAOUiManager getInstance() {
        if (instance == null) {
            instance = new AHAOUiManager();
        }
        return instance;
    }

    public void showLogin(Context context) {
        CustomLoginDialog.showLoginDialog(context, new CustomLoginDialog.LoginResultListener() {
            @Override
            public void onLoginSuccess(String username, String password) {
                SimpleHttpGetRequest httpRequest = new SimpleHttpGetRequest(new SimpleHttpGetRequest.OnHttpRequestCompleteListener() {
                    @Override
                    public void onHttpRequestComplete(String result) {
                        // 在这里处理 HTTP 请求的结果
                        try {
                            if (result != null) {
                                // 处理成功的情况
                                // result 包含服务器响应的内容
                                JSONObject jsonObject = new JSONObject(result);
                                int code = jsonObject.getInt("code");
                                String msg = jsonObject.getString("msg");
                                if (code == CONSTANT.SUCCESS) {
                                    JSONObject data = jsonObject.getJSONObject("data");
                                    LoginResult loginResult = new LoginResult();
                                    loginResult.setUsername(data.getString("username"));
                                    loginResult.setUserId(data.getString("uid"));
                                    LogUtils.info("token:" + data.getString("token"));
                                    loginResult.setToken(data.getString("token"));
                                    AHSDKManager.getInstance().getSDKListener().onLoginResult(CONSTANT.SUCCESS, loginResult);
                                    UserInfo.getInstance().setLoginResult(loginResult);
                                } else {
                                    AHSDKManager.getInstance().getSDKListener().onLoginResult(CONSTANT.ERROR, msg);
                                    AHSDKManager.showToast(context, msg);
                                }
                            } else {
                                // 处理失败的情况
                                AHSDKManager.getInstance().getSDKListener().onLoginResult(CONSTANT.ERROR, "网络请求失败");
                                AHSDKManager.showToast(context, "网络请求失败");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            AHSDKManager.getInstance().getSDKListener().onLoginResult(CONSTANT.ERROR, e.getMessage());
                        }
                    }
                });
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        httpRequest.execute(String.format("/v1.jh_login?username=%s&password=%s", username, password));
                    }
                }).start();
            }

            @Override
            public void onLoginFailure() {
                AHSDKManager.getInstance().getSDKListener().onLoginResult(CONSTANT.ERROR, "登录失败");
            }
        });
    }

    public void showPay(Context context, PayParams payParams) {
        String amount = payParams.amount;
        String roleName = payParams.roleName;
        String roleId = payParams.roleId;
        String serverName = payParams.serverName;
        String serverId = payParams.serverId;
        String roleLevel = payParams.roleLevel;
        String power = payParams.power;
        String vipLevel = payParams.vipLevel;
        String notifyUrl = payParams.notifyUrl;
        String orderid = payParams.orderid;
        String htmlContent = "<p>请确认支付信息：</p>" +
                "<p>订单号：" + orderid + "</p>" +
                "<p>金额：" + amount + "</p>" +
                "<p>角色名：" + roleName + "</p>" +
                "<p>角色ID：" + roleId + "</p>" +
                "<p>服务器名：" + serverName + "</p>" +
                "<p>服务器ID：" + serverId + "</p>" +
                "<p>角色等级：" + roleLevel + "</p>" +
                "<p>战力：" + power + "</p>" +
                "<p>VIP等级：" + vipLevel + "</p>" +
                "<p>支付结果通知地址：" + notifyUrl + "</p>";
        String payParamsParams = String.format("amount=%s&roleName=%s&roleId=%s&serverName=%s&serverId=%s&roleLevel=%s&power=%s&vipLevel=%s&notifyUrl=%s&orderid=%s", amount, roleName, roleId, serverName, serverId, roleLevel, power, vipLevel, notifyUrl,orderid);
        CustomPaymentDialog.showPaymentDialog(context, htmlContent, new CustomPaymentDialog.PaymentResultListener() {
            @Override
            public void onPaymentSuccess() {
                SimpleHttpGetRequest httpRequest = new SimpleHttpGetRequest(new SimpleHttpGetRequest.OnHttpRequestCompleteListener() {
                    @Override
                    public void onHttpRequestComplete(String result) {
                        // 在这里处理 HTTP 请求的结果
                        try {
                            if (result != null) {
                                // 处理成功的情况
                                // result 包含服务器响应的内容
                                JSONObject jsonObject = new JSONObject(result);
                                int code = jsonObject.getInt("code");
                                String msg = jsonObject.getString("msg");
                                if (code == CONSTANT.SUCCESS) {
                                    AHSDKManager.getInstance().getSDKListener().onPayResult(CONSTANT.SUCCESS, "支付成功");
                                    AHSDKManager.showToast(context, "支付成功");

                                } else {
                                    AHSDKManager.getInstance().getSDKListener().onPayResult(CONSTANT.ERROR, msg);
                                    AHSDKManager.showToast(context, "支付失败:" + msg);
                                }
                            } else {
                                // 处理失败的情况
                                AHSDKManager.getInstance().getSDKListener().onPayResult(CONSTANT.ERROR, "网络请求失败");
                                AHSDKManager.showToast(context, "网络请求失败");

                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            AHSDKManager.getInstance().getSDKListener().onPayResult(CONSTANT.ERROR, e.getMessage());
                            AHSDKManager.showToast(context, "支付发生异常:" + e.getMessage());
                        }
                    }
                });
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        httpRequest.execute("/v1.jh_pay?status=1", payParams);
                    }
                }).start();
            }

            @Override
            public void onPaymentFailure() {
                SimpleHttpGetRequest httpRequest = new SimpleHttpGetRequest(new SimpleHttpGetRequest.OnHttpRequestCompleteListener() {
                    @Override
                    public void onHttpRequestComplete(String result) {
                        // 在这里处理 HTTP 请求的结果
                        try {
                            if (result != null) {
                                // 处理成功的情况
                                // result 包含服务器响应的内容
                                JSONObject jsonObject = new JSONObject(result);
                                int code = jsonObject.getInt("code");
                                String msg = jsonObject.getString("msg");
                                if (code == CONSTANT.SUCCESS) {
                                    AHSDKManager.getInstance().getSDKListener().onPayResult(CONSTANT.SUCCESS, "支付失败");
                                    AHSDKManager.showToast(context, "通知支付结果“失败”成功");
                                } else {
                                    AHSDKManager.getInstance().getSDKListener().onPayResult(CONSTANT.ERROR, msg);
                                    AHSDKManager.showToast(context, "通知支付结果“失败“不成功：" + msg);
                                }
                            } else {
                                // 处理失败的情况
                                AHSDKManager.getInstance().getSDKListener().onPayResult(CONSTANT.ERROR, "网络请求失败");
                                AHSDKManager.showToast(context, "网络请求失败");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            AHSDKManager.getInstance().getSDKListener().onPayResult(CONSTANT.ERROR, e.getMessage());
                            AHSDKManager.showToast(context, "通知支付结果“失败“发生异常：" + e.getMessage());

                        }
                    }
                });
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        httpRequest.execute(String.format("/v1.jh_pay?status=0&%s", payParamsParams));
                    }
                }).start();
            }
        });
    }

    public void showFloatView(Context context){
        FloatViewManager.getInstance().showFloatView(context);
    }

    public void hideFloatView(){
        FloatViewManager.getInstance().hideFloatView();
    }

}
